<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="150px"
      v-loading="formLoading"
    >
      <el-form-item label="客户编号" prop="customerId">
        <el-input v-model="formData.customerId" placeholder="请输入客户编号" />
      </el-form-item>
      <el-form-item label="地址类型" prop="addressType">
        <el-select v-model="formData.addressType" placeholder="请选择地址类型">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CROSSBORDER_ADDRESS_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="地址名称" prop="addressName">
        <el-input v-model="formData.addressName" placeholder="请输入地址名称" />
      </el-form-item>
      <el-form-item label="名" prop="firstName">
        <el-input v-model="formData.firstName" placeholder="请输入名" />
      </el-form-item>
      <el-form-item label="姓" prop="lastName">
        <el-input v-model="formData.lastName" placeholder="请输入姓" />
      </el-form-item>
      <el-form-item label="全名" prop="fullName">
        <el-input v-model="formData.fullName" placeholder="请输入全名" />
      </el-form-item>
      <el-form-item label="公司" prop="company">
        <el-input v-model="formData.company" placeholder="请输入公司" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="formData.phone" placeholder="请输入电话" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formData.email" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item label="国家代码" prop="countryCode">
        <el-input v-model="formData.countryCode" placeholder="请输入国家代码" />
      </el-form-item>
      <el-form-item label="国家名称" prop="countryName">
        <el-input v-model="formData.countryName" placeholder="请输入国家名称" />
      </el-form-item>
      <el-form-item label="省份" prop="province">
        <el-input v-model="formData.province" placeholder="请输入省份" />
      </el-form-item>
      <el-form-item label="城市" prop="city">
        <el-input v-model="formData.city" placeholder="请输入城市" />
      </el-form-item>
      <el-form-item label="区县" prop="district">
        <el-input v-model="formData.district" placeholder="请输入区县" />
      </el-form-item>
      <el-form-item label="地址行1" prop="addressLine1">
        <el-input v-model="formData.addressLine1" placeholder="请输入地址行1" />
      </el-form-item>
      <el-form-item label="地址行2" prop="addressLine2">
        <el-input v-model="formData.addressLine2" placeholder="请输入地址行2" />
      </el-form-item>
      <el-form-item label="邮政编码" prop="postalCode">
        <el-input v-model="formData.postalCode" placeholder="请输入邮政编码" />
      </el-form-item>
      <el-form-item label="纬度" prop="latitude">
        <el-input v-model="formData.latitude" placeholder="请输入纬度" />
      </el-form-item>
      <el-form-item label="经度" prop="longitude">
        <el-input v-model="formData.longitude" placeholder="请输入经度" />
      </el-form-item>
      <el-form-item label="是否默认地址" prop="isDefault">
        <el-radio-group v-model="formData.isDefault">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否已验证" prop="isValidated">
        <el-radio-group v-model="formData.isValidated">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="验证来源" prop="validationSource">
        <el-input v-model="formData.validationSource" placeholder="请输入验证来源" />
      </el-form-item>
      <el-form-item label="验证时间" prop="validationTime">
        <el-date-picker
          v-model="formData.validationTime"
          type="date"
          value-format="x"
          placeholder="选择验证时间"
        />
      </el-form-item>
      <el-form-item label="使用次数" prop="useCount">
        <el-input v-model="formData.useCount" placeholder="请输入使用次数" />
      </el-form-item>
      <el-form-item label="最后使用时间" prop="lastUseTime">
        <el-date-picker
          v-model="formData.lastUseTime"
          type="date"
          value-format="x"
          placeholder="选择最后使用时间"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { OrderCustomerAddressApi, OrderCustomerAddressVO } from '@/api/crossborder/ordercustomeraddress'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

/** 跨境电商客户地址 表单 */
defineOptions({ name: 'OrderCustomerAddressForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  customerId: undefined,
  addressType: undefined,
  addressName: undefined,
  firstName: undefined,
  lastName: undefined,
  fullName: undefined,
  company: undefined,
  phone: undefined,
  email: undefined,
  countryCode: undefined,
  countryName: undefined,
  province: undefined,
  city: undefined,
  district: undefined,
  addressLine1: undefined,
  addressLine2: undefined,
  postalCode: undefined,
  latitude: undefined,
  longitude: undefined,
  isDefault: undefined,
  isValidated: undefined,
  validationSource: undefined,
  validationTime: undefined,
  useCount: undefined,
  lastUseTime: undefined,
})
const formRules = reactive({
  customerId: [{ required: true, message: '客户编号不能为空', trigger: 'blur' }],
  addressType: [{ required: true, message: '地址类型不能为空', trigger: 'change' }],
  firstName: [{ required: true, message: '名不能为空', trigger: 'blur' }],
  lastName: [{ required: true, message: '姓不能为空', trigger: 'blur' }],
  fullName: [{ required: true, message: '全名不能为空', trigger: 'blur' }],
  countryCode: [{ required: true, message: '国家代码不能为空', trigger: 'blur' }],
  city: [{ required: true, message: '城市不能为空', trigger: 'blur' }],
  addressLine1: [{ required: true, message: '地址行1不能为空', trigger: 'blur' }],
  postalCode: [{ required: true, message: '邮政编码不能为空', trigger: 'blur' }],
  isDefault: [{ required: true, message: '是否默认地址不能为空', trigger: 'blur' }],
  isValidated: [{ required: true, message: '是否已验证不能为空', trigger: 'blur' }],
  useCount: [{ required: true, message: '使用次数不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await OrderCustomerAddressApi.getOrderCustomerAddress(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as OrderCustomerAddressVO
    if (formType.value === 'create') {
      await OrderCustomerAddressApi.createOrderCustomerAddress(data)
      message.success(t('common.createSuccess'))
    } else {
      await OrderCustomerAddressApi.updateOrderCustomerAddress(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    customerId: undefined,
    addressType: undefined,
    addressName: undefined,
    firstName: undefined,
    lastName: undefined,
    fullName: undefined,
    company: undefined,
    phone: undefined,
    email: undefined,
    countryCode: undefined,
    countryName: undefined,
    province: undefined,
    city: undefined,
    district: undefined,
    addressLine1: undefined,
    addressLine2: undefined,
    postalCode: undefined,
    latitude: undefined,
    longitude: undefined,
    isDefault: undefined,
    isValidated: undefined,
    validationSource: undefined,
    validationTime: undefined,
    useCount: undefined,
    lastUseTime: undefined,
  }
  formRef.value?.resetFields()
}
</script>